package com.ctgu.fastleader.controller;

import com.ctgu.fastleader.service.UserService;
import com.ctgu.fastleader.vo.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@Controller
public class LoginController {

    @Resource
    UserService userService;

    @RequestMapping("/logout")
    public String toLoginPage(HttpServletRequest request){
        Object admin = request.getSession().getAttribute("userid");
        if(admin != null) {
//            登出时删除session中的该用户信息,同时刻只会存在一个用户
            request.getSession().removeAttribute("userid");
        }
        return "login";
    }

    @RequestMapping("/login")
    public String logIn(Integer uid, String pwd, HttpServletRequest request){

        User user = userService.selectByPrimaryKey(uid);
        if(user.getUpassword().equals(pwd)){
            request.getSession().setAttribute("userid",uid);
            return "navigation";
        }
        else{
            return "redirect:";
        }
    }

}
